package com.hzhc.controller.trainplan;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.TrainPlan;
import com.hzhc.entity.TrainPlanDetail;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.*;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.*;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/5/30 13:33
 * @description TrainPlanDetailController
 */
@RestController
@RequestMapping("/trainPlanDetail")
@RequiredArgsConstructor
public class TrainPlanDetailController extends BaseController {

    private final TrainPlanDetailService detailService;

    private final CntrInfoService infoService;

    private final TrainPlanService planService;

    private final VehicleReservationService reservationService;

    private final YwOrderCntrService ywOrderCntrService;

    private final TbSequenceService sequenceService;

    /**
     * 装车计划-录入装车清单-箱子列表 （非直装）
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getCntrInfoAll")
    public ResultUtil<List<CntrInfoVo>> getCntrInfoAll(HttpServletRequest request, @RequestBody CntrInfoVo vo){
        List<CntrInfoVo> list = infoService.getCntrInfoAll(vo);
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 装车计划-录入装车清单-箱子列表(直装)
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getZzCntrInfoAll")
    public ResultUtil<List<CntrInfoVo>> getZzCntrInfoAll(HttpServletRequest request, @RequestBody CntrInfoVo vo){
        List<CntrInfoVo> list = infoService.getZzCntrInfoAll(vo);//散改集下箱子(直装)
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 装车计划-录入装车清单-绑定组别
     * @param request
     * @param detail
     * @return
     */
    @PostMapping("/saveDetailMsg")
    @SysTemLog(operModul = "调度管理-装车计划-选择装箱清单",operType = "ADD",operDesc = "计划下箱子添加",modular="PC")
    public ResultUtil<T> saveDetailMsg(HttpServletRequest request, @RequestBody TrainPlanDetail detail){
        Long userID = TokenUtil.getUserID(request);
        detail.setUserId(userID);
        return detailService.saveDetailMsg(detail);
    }

    /**
     * 装车计划-录入装车清单-单次录入
     * @param request
     * @param detail
     * @return
     */
    @PostMapping("/saveDetailMsgOne")
    @SysTemLog(operModul = "调度管理-装车计划-选择装箱清单",operType = "ADD",operDesc = "计划下箱子添加",modular="PC")
    public ResultUtil<T> saveDetailMsgOne(HttpServletRequest request, @RequestBody TrainPlanDetail detail){
        Long userID = TokenUtil.getUserID(request);
        detail.setUserId(userID);
        return detailService.saveDetailMsgOne(detail);
    }

    /**
     * 装车计划-详情-班列箱信息展示
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getTrainPlanDetailList")
    @ResponseBody
    public ResultUtil<List<TrainPlanDetailVo>> getTrainPlanDetailList(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        List<TrainPlanDetailVo> list = detailService.getTrainPlanDetailList(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 下发装车指令前-判断有无选择(绑定)箱子
     * @param id
     * @return
     */
    @GetMapping("/checkDetailExist/{id}")
    public ResultUtil<Integer> checkDetailExist(@PathVariable Long id){
        Integer count = detailService.checkDetailExist(id);
        return ResultUtil.ok(count);
    }

    //装车计划详情-删除单个数据(解绑)
    @RequestMapping("/deleteDetail")
    public ResultUtil<T> deleteDetail(HttpServletRequest request,@RequestBody CntrLogVo vo){
        return detailService.deleteDetail(request,vo);
    }

    /**
     * 装车理货  列表list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getAllTrainPlanDetail")
    @ResponseBody
    public ResultUtil<List<TrainPlanDetailVo>> getAllTrainPlanDetail(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        Page<TrainPlanDetailVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<TrainPlanDetailVo> list = new ArrayList<>();
        if (vo.getIsZzt()==0){
            list = detailService.getAllTrainPlanDetail(page,vo);//(出场)
        }else{
            list = detailService.getZzTrainPlanDetail(page,vo);//直装
        }
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 装车-理货
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/carTally")
    public ResultUtil<T> carTally(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        UserDto user = TokenUtil.getUser(request);
        vo.setUserId(user.getId());
        vo.setUserName(user.getRealname());
        vo.setAccesspath("PC");
        if (vo.getIsRectified()==0){
            return detailService.carTally(vo);//正常装车理货
        }else{
            return detailService.carTallyDzg(vo);//是待整改
        }
    }

    /**
     * 卸车清单箱信息导入---(不用)
     * @param request
     * @param planId
     * @param list
     * @return
     */
    @PostMapping("/importTrainPlanCntr/{planId}")
    @SysTemLog(operModul = "调度管理-装卸车计划-卸车清单箱信息导入",operType = "IMPORT",operDesc = "卸车清单箱信息导入功能",modular="PC")
    public ResultUtil<T> importTrainPlanCntr(HttpServletRequest request, @PathVariable("planId") Integer planId, @RequestBody List<TrainPlanDetail> list){
        UserDto user = TokenUtil.getUser(request);
        TrainPlan plan = planService.getById(planId);
        for (TrainPlanDetail detail : list) {
            if (user != null) {
                detail.setPlanId(planId).setUserId(user.getId()).setUserName(user.getRealname()).setAccesspath("PC端")
                        .setCreatetime(LocalDateTime.now()).setOrderNo(plan.getTrainPlanNo());
            }
        }
        return detailService.saveTrainPlanDetailList(planId,list);
    }

    /**
     * 卸车导入清单 check
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/cntrUploadCheck")
    public ResultUtil<List<TrainPlanDetailVo>> cntrUploadCheck(MultipartFile file, HttpServletRequest request){
        UserDto user = TokenUtil.getUser(request);
        return detailService.cntrUploadCheck(file);
    }

    /**
     * 卸车数据池 箱信息导入
     * @param request
     * @param list
     * @return
     */
    @PostMapping("/importXcTrainPlanCntr")
    @SysTemLog(operModul = "调度管理-装卸车计划-卸车清单箱信息导入",operType = "IMPORT",operDesc = "卸车清单箱信息导入功能",modular="PC")
    public ResultUtil<T> importXcTrainPlanCntr(HttpServletRequest request,  @RequestBody List<TrainPlanDetail> list){
        return detailService.importXcTrainPlanCntr(request,list);
    }

    /**
     * 卸车理货(入场)  列表list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getXcTrainPlanDetail")
    @ResponseBody
    public ResultUtil<List<TrainPlanDetailVo>> getZcTrainPlanDetail(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        Page<TrainPlanDetailVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<TrainPlanDetailVo> list = detailService.getXcTrainPlanDetail(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 卸车理货(直提)  列表list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getZtTrainPlanDetail")
    @ResponseBody
    public ResultUtil<List<TrainPlanDetailVo>> getZtTrainPlanDetail(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        Page<TrainPlanDetailVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<TrainPlanDetailVo> list = detailService.getZtTrainPlanDetail(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 卸车理货
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/xcTally")
    public ResultUtil<T> xcTally(HttpServletRequest request,@RequestBody CntrTallyVo vo){
        UserDto user = TokenUtil.getUser(request);
        vo.setUserId(user.getId());
        vo.setUserName(user.getRealname());
        vo.setAccesspath("PC");
        if (vo.getIsZzt()==0){
            return infoService.cntrXcTally(vo);//普通卸车
        }
        return infoService.cntrXcTallyZt(vo);//直提卸车
    }

    /**
     * 堆存计划-卸车箱子清单列表
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getXcCntrList")
    public ResultUtil<List<TrainPlanDetail>> getXcCntrList(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        Page<TrainPlanDetailVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<TrainPlanDetail> list = detailService.getXcCntrList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 新增箱信息
     */
    @PostMapping("/addDetailRet")
    public ResultUtil<T> addDetailRet(@RequestBody TrainPlanDetail detail, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        detail.setUserId(user.getId());
        detail.setUserName(user.getRealname());
        detail.setCreatetime(LocalDateTime.now());
        return detailService.addDetailRet(detail);
    }

    /**
     * 编辑箱信息
     */
    @PostMapping("/updateDetailById")
    public ResultUtil<T> updateDetailById(@RequestBody TrainPlanDetail detail) {
        return detailService.updateDetailById(detail);
    }

    /**
     * (可批量)
     * 删除箱信息
     */
    @PostMapping("/delBatchDetailRet")
    public ResultUtil<T> delBatchDetailRet(@RequestBody TrainPlanVo vo, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        vo.setUpdateUserId(user.getId());
        vo.setUpdateTime(LocalDateTime.now());
        return detailService.delBatchDetailRet(vo);
    }

    //卸车数据池删除清单单个信息
    @GetMapping("/deleteXcCntr/{id}")
    public ResultUtil<T> deleteXcCntr(@PathVariable Long id){
        return detailService.deleteXcCntr(id);
    }

    //卸车数据池 修改单个信息
    @RequestMapping("/updatePoolCntrMsg")
    public ResultUtil<T> updatePoolCntrMsg(HttpServletRequest request,@RequestBody TrainPlanDetail detail){
        Long userID = TokenUtil.getUserID(request);
        return detailService.updatePoolCntrMsg(detail);
    }

    /**
     * 根据卸车计划id获取清单中的公司信息
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/getXcTrainPlanDetailCompanyList")
    public ResultUtil<List<CompanyInfoVo>> getXcTrainPlanDetailCompanyList(HttpServletRequest request, @RequestBody TrainPlanDetailVo vo) {
        List<CompanyInfoVo> list = detailService.getXcTrainPlanDetailCompanyList(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    //卸车数据池数据
    @RequestMapping("/getXcDataPoolList")
    public ResultUtil<List<TrainPlanDetailVo>> getXcDataPoolList(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        Page<TrainPlanDetailVo> page = new Page<TrainPlanDetailVo>(vo.getPageIndex(),vo.getPageSize());
        List<TrainPlanDetailVo> list = detailService.getXcDataPoolList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    //卸车直提 回显 车辆预约信息
    @RequestMapping("/getXcZtMsg")
    public ResultUtil<VehicleReservationVo> getXcZtMsg(HttpServletRequest request,@RequestBody VehicleReservationVo vo){
        VehicleReservationVo rel = reservationService.getXcZtMsg(vo);
        return ResultUtil.build(rel,ResultUtilCodeEnum.SUCCESS);
    }

    //卸车理货 卸车班列选择
    @GetMapping("/getXcPlanList")
    public ResultUtil<List<TrainPlan>> getXcPlanList(){
        List<TrainPlan> list = planService.getXcPlanList();
        return ResultUtil.build(list,ResultUtilCodeEnum.SUCCESS);
    }

    //批量修改收货人信息
    @RequestMapping("/updateConsigneeMsg")
    public ResultUtil<T> updateConsigneeMsg(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        return detailService.updateConsigneeMsg(vo);
    }


    /**
     * 装车计划班列箱信息 - 导出
     */
    @RequestMapping( "/exportTrainPlanDetailReport")
    @SysTemLog(operModul = "装车计划班列箱信息-导出", operType = "导出", operDesc = "装车计划班列箱信息导出功能", modular = "PC")
    public void exportTrainPlanDetailReport(TrainPlanDetailVo vo, HttpServletRequest request, HttpServletResponse response) {
        if(StrUtil.isBlank(vo.getCntrNo())){
            vo.setCntrNo("");
        }
        if(StrUtil.isBlank(vo.getConsigner())){
            vo.setConsigner("");
        }
        detailService.exportTrainPlanDetailReport(vo,request,response);
    }

    /**
     * 装/卸 清单列表
     */
    @RequestMapping("/getZcAndXcDetail")
    public ResultUtil<List<TrainPlanDetailVo>> getZcAndXcDetail(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        Long userID = TokenUtil.getUserID(request);
        Page<TrainPlanDetailVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<TrainPlanDetailVo> list = detailService.getZcAndXcDetail(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 到达箱推送 列表(卸车完工，收货人包含 新丝路 在场箱 )
     */
    @RequestMapping("/getDdCntrPush")
    public ResultUtil<List<TrainPlanDetailVo>> getDdCntrPush(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        Page<TrainPlanDetailVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<TrainPlanDetailVo> list = detailService.getDdCntrPush(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 批量 推送  修改收货人信息(卸车清单+箱信息+箱动态)
     *
     */
    @RequestMapping("/updateConsignee")
    public ResultUtil<T> updateConsignee(HttpServletRequest request,@RequestBody DdPushCntrVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        String date = DateUtil.format(new Date(), "YYYY");
        String num = sequenceService.hcseq_next_dayvalSuffix(3, "trainplanid");
        vo.setTsNum(Integer.parseInt((date+num)));//批量推送组别号
        return detailService.updateConsignee(vo);
    }

    /**
     * 装卸清单--导出
     * @param request
     * @param response
     * @param vo
     */
    @GetMapping("/exportZxDetail")
    @SysTemLog(operModul = "装卸清单导出", operType = "导出", operDesc = "装卸清单导出功能", modular="PC")
    public void exportZxDetail(HttpServletRequest request, HttpServletResponse response,TrainPlanDetailVo vo) {
        detailService.exportZxDetail(request, response, vo);
    }

    /**
     * 车次完工
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/updateCntrWg")
    public ResultUtil<T> updateCntrWg(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo) {
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        return detailService.updateCntrWg(vo);
    }


    /**
     * 装车计划导入清单
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/zcCntrUploadCheck")
    public ResultUtil<List<TrainPlanDetailVo>> zcCntrUploadCheck(MultipartFile file, HttpServletRequest request){
        UserDto user = TokenUtil.getUser(request);
        return detailService.zcCntrUploadCheck(file);
    }

    /**
     * 装车箱信息导入
     * @param request
     * @return
     */
    @PostMapping("/importZcCntr")
    @SysTemLog(operModul = "调度管理-装卸车计划-装车箱信息导入",operType = "IMPORT",operDesc = "装车箱信息导入功能",modular="PC")
    public ResultUtil<T> importZcCntr(@RequestBody TrainPlanVo vo,HttpServletRequest request){
        return detailService.importZcCntr (request,vo);
    }
}
